Dependencies
In [100]:
from __future__ import division
from itertools import *
In [1]:
import os
falange = r"/run/user/1000/gvfs/smb-share:server=falange,share=homes"
#os.listdir(falange)
In [2]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib nbagg
$ ipcluster start -n 4
In [3]:
import matplotlib.patches as mpatches
import seaborn as sns
from matplotlib.colors import ListedColormap
sns.set_palette("Spectral")
current_palette = sns.color_palette()
my_cmap = ListedColormap((["#000000"]+current_palette.as_hex())[::-1])
In [9]:
from ipyparallel import Client
rc = Client(profile="parallel")
In [20]:
rc.shutdown()
In [10]:
dv = rc[:]
In [11]:
dv
Out[11]:
In [12]:
rc.ids
Out[12]:
In [13]:
with dv.sync_imports():
import numpy
In [9]:
@dv.parallel(block = True)
def mandel3(x, y, max_iters=50):
c = complex(x, y)
z = 0.0j
for i in range(max_iters):
z = z*z + c
if z.real*z.real + z.imag*z.imag >= 4:
return i
return max_iters #could return X,Y, max_iters to try block=False
In [29]:
#zooming
def zoombrot(zooms, n=500, target = [-0.748202,-0.161082]):
for zoom in range(zooms):
window = 1./10**zoom
x = np.linspace(target[0]-1*window,target[0]+1*window,n)
y = np.linspace(target[1]-1*window,target[1]+1*window,n)
X, Y = np.meshgrid(x, y)
im3 = numpy.reshape(mandel3.map(X.ravel(), Y.ravel()), (len(y), len(x)))
fig, axes = plt.subplots(1, 1)
axes.grid(False)
values = np.unique(im3.ravel())
sns.set_palette("Spectral",len(values)-1)
current_palette = sns.color_palette()
my_cmap = ListedColormap((["#000000"]+current_palette.as_hex())[::-1])
im = axes.imshow(im3, cmap=my_cmap,extent=[x[0],x[-1],y[0],y[-1]])
#im = axes.imshow(ship, cmap=my_cmap)
#colors = [ im.cmap(im.norm(value)) for value in values]
#patches = [ mpatches.Patch(color=colors[i], label="{l}".format(l=values[i]) ) for i in range(len(values)) ]
#plt.legend(handles=patches, loc='best', title="Iterations", frameon=True, fancybox=True)
plt.title('Mandelbrot w/ %s Points'%(len(im3)**2))
plt.show()
In [30]:
zoombrot(5)
In [15]:
@dv.parallel(block=True)
def burning_ship(x,y,max_iters=100):
c = complex(x,y)
z = 0.0j
for i in range(max_iters):
z = (abs(z.real)+abs(z.imag)*1j)**2 + c
if z.real*z.real + z.imag*z.imag >= 4:
return i
return max_iters #could return X,Y, max_iters to try block=False
In [219]:
#x = np.arange(-2, 1, 0.0005)
#y = np.arange(-1, 1, 0.0005)
n = 5000
n = 10000
#n = 500
y = np.linspace(-0.015,0.0025,1/3 * n)
x = np.linspace(-1.87,-1.83,n)
xstart = -1.871
x = np.linspace(xstart,xstart+(y[-1]-y[0])*3,n)
X, Y = np.meshgrid(x, y)
In [220]:
%%time
shipwide = numpy.reshape(burning_ship.map(X.ravel(), Y.ravel()), (len(y), len(x)))
In [221]:
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
def colour_grad(cs,n):
grad = []
for c1,c2 in pairwise(cs):
for step in np.linspace(0,1,n/(len(cs)-1),endpoint=False):
grad += [tuple(c1*step + c2*(1-step))]
return grad
In [222]:
sns.set_palette(['orange','white'])
sns.set_palette(colour_grad(np.array(sns.color_palette()),len(np.unique(shipwide))))
In [223]:
#sns.set_palette("YlOrRd_d",len(np.unique(shipwide)))
sns.set_palette("OrRd",len(np.unique(shipwide)))
current_palette = sns.color_palette()
#my_cmap = ListedColormap((["#000000"]+current_palette.as_hex()[::-1])[::-1])
#my_cmap = ListedColormap(current_palette.as_hex()[::-1])
my_cmap = ListedColormap(current_palette.as_hex())
In [224]:
fig, axes = plt.subplots(1, 1)
axes.grid(False)
im = axes.imshow(shipwide, cmap=my_cmap,extent=[x[0],x[-1],y[-1],y[0]],interpolation='bessel')
axes.set_xticks([])
axes.set_yticks([])
fig.tight_layout()
fig.savefig('burningshipwide.png', format='png', dpi=1200,bbox_inches='tight')
#plt.show()
In [143]:
%%time
im3 = numpy.reshape(mandel3.map(X.ravel(), Y.ravel()), (len(y), len(x)))
In [144]:
fig, axes = plt.subplots(1, 1)
axes.grid(False)
# im = axes.imshow(im3, cmap=my_cmap)
im = axes.imshow(ship, cmap=my_cmap)
axes.set_xticks([])
axes.set_yticks([])
if legending:
values = np.unique(im)
# get the colors of the values, according to the
# colormap used by imshow
colors = [ im.cmap(im.norm(value)) for value in values]
# create a patch (proxy artist) for every color
patches = [ mpatches.Patch(color=colors[i*10], label="{l}".format(l=values[i*10]) ) for i in range(len(values)/10 +1) ]
# put those patched as legend-handles into the legend
plt.legend(handles=patches, loc='best', title="Iterations", frameon=True, fancybox=True)
plt.title('Mandelbrot w/ %s Points'%(len(im3)**2))
fig.
plt.show()
In [46]:
%%time
%px a = numpy.random.random(1000)
print np.array(dv['a']).mean()
Parallel Magic Funcs
In [44]:
%%px
%%time
%matplotlib inline
import seaborn as sns
x = numpy.random.normal(numpy.random.randint(-10, 10), 1, 1000)
sns.kdeplot(x);